package com.love.cloud.gateway.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.love.cloud.gateway.filter.PasswordDecoderFilter;
import com.love.cloud.gateway.filter.PigRequestGlobalFilter;
import com.love.cloud.gateway.filter.ValidateCodeGatewayFilter;
import com.love.cloud.gateway.handler.GlobalExceptionHandler;
import com.love.cloud.gateway.handler.ImageCodeHandler;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 网关配置
 *
 * @author L.cm
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(GatewayConfigProperties.class)
public class GatewayConfiguration {

	@Bean
	public PasswordDecoderFilter passwordDecoderFilter(GatewayConfigProperties configProperties) {
		return new PasswordDecoderFilter(configProperties);
	}

//	@Bean
//	public CorsFilter corsFilter() {
//		CorsConfiguration config = new CorsConfiguration();
//		config.setAllowCredentials(true);
//		config.addAllowedOriginPattern("*");
//		config.addAllowedHeader("*");
//		config.setMaxAge(18000L);
//		config.addAllowedMethod("*");
//		org.springframework.web.cors.UrlBasedCorsConfigurationSource source = new org.springframework.web.cors.UrlBasedCorsConfigurationSource();
//		source.registerCorsConfiguration("/**", config);
//		return new CorsFilter(source);
//	}

//	@Bean
//	public WebFilter corsFilter2() {
//		return (ServerWebExchange ctx, WebFilterChain chain) -> {
//			ServerHttpRequest request = ctx.getRequest();
//			if (CorsUtils.isCorsRequest(request)) {
//				HttpHeaders requestHeaders = request.getHeaders();
//				ServerHttpResponse response = ctx.getResponse();
//				HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
//				HttpHeaders headers = response.getHeaders();
//				headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
//				headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS,
//						requestHeaders.getAccessControlRequestHeaders());
//				if (requestMethod != null) {
//					headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
//				}
//				headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
//				headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
//				if (request.getMethod() == HttpMethod.OPTIONS) {
//					response.setStatusCode(HttpStatus.OK);
//					return Mono.empty();
//				}
//			}
//			return chain.filter(ctx);
//		};
//	}


	@Bean
	public PigRequestGlobalFilter pigRequestGlobalFilter() {
		return new PigRequestGlobalFilter();
	}

	@Bean
	public ValidateCodeGatewayFilter validateCodeGatewayFilter(GatewayConfigProperties configProperties,
			ObjectMapper objectMapper, RedisTemplate redisTemplate) {
		return new ValidateCodeGatewayFilter(configProperties, objectMapper, redisTemplate);
	}

	@Bean
	public GlobalExceptionHandler globalExceptionHandler(ObjectMapper objectMapper) {
		return new GlobalExceptionHandler(objectMapper);
	}

	@Bean
	public ImageCodeHandler imageCodeHandler(RedisTemplate redisTemplate) {
		return new ImageCodeHandler(redisTemplate);
	}

}
